//
// DirectoryIterator_UNIX.cpp
//
// $Id: //poco/1.4/Foundation/src/DirectoryIterator_UNIX.cpp#2 $
//
// Library: Foundation
// Package: Filesystem
// Module:  DirectoryIterator
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/DirectoryIterator_UNIX.h"
#include "Poco/File.h"

namespace Poco {
	
	
//
// inlines
//
const std::string& DirectoryIteratorImpl::get() const
{
	return m_current;
}


inline void DirectoryIteratorImpl::duplicate()
{
	++m_rc;
}


inline void DirectoryIteratorImpl::release()
{
	if (--m_rc == 0)
		delete this;
}

DirectoryIteratorImpl::DirectoryIteratorImpl(const std::string& path): m_pDir(0), m_rc(1)
{
	Path p(path);
	p.makeFile();
	m_pDir = opendir(p.toString().c_str());
	if (!m_pDir) File::handleLastError(path);
	next();
}


DirectoryIteratorImpl::~DirectoryIteratorImpl()
{
	if (m_pDir) closedir(m_pDir);
}


const std::string& DirectoryIteratorImpl::next()
{
	do
	{
		struct dirent* pEntry = readdir(m_pDir);
		if (pEntry)
			m_current = pEntry->d_name;
		else
			m_current.clear();
	}
	while (m_current == "." || m_current == "..");
	return m_current;
}


} // namespace Poco
